<template>
  <div class="add-wrapper">
    <Dialog ref="dialog" class="dialog-wrapper" @closeDialog="handleCloseDialog('ruleForm')">
      <template>
        <div>
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            :rules="rules"
            label-width="100px"
            class="demo-ruleForm"
          >
            <el-form-item label="中文名称：" prop="zh_name">
              <el-input v-model="ruleForm.zh_name" class="el-290" />
            </el-form-item>
            <el-form-item label="英文名称：" prop="en_name">
              <el-input v-model="ruleForm.en_name" class="el-290" />
            </el-form-item>
            <el-form-item label="缩写：" prop="brand_name">
              <el-input v-model="ruleForm.brand_name" class="el-290" />
            </el-form-item>
            <el-form-item label="图片：" prop="brand_img">
              <el-upload
                :limit="1"
                action="https://wx.chengkenet.com/api/common/upload/"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
                :file-list="fileList"
              >
                <div slot="tip" class="el-upload__tip color-red">（建议尺寸：90px*90px）</div>
                <i class="el-icon-plus" />
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="ruleForm.brand_img" alt>
              </el-dialog>
            </el-form-item>
          </el-form>
        </div>
      </template>
    </Dialog>
  </div>
</template>
<script>
import Dialog from '@/common/dialog'
const Err_OK = 200
export default {
  components: {
    Dialog
  },
  props: {
    listItem: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      dialogVisible: false,
      fileList: [],
      ruleForm: {
        en_name: '',
        zh_name: '',
        brand_name: '',
        brand_img: ''
      },
      rules: {
        en_name: [
          { required: true, message: '请输入英文名称', trigger: 'blur' }
        ],
        zh_name: [
          { required: true, message: '请输入中文名称', trigger: 'blur' }
        ],
        brand_name: [
          { required: true, message: '请输入缩写', trigger: 'blur' }
        ],
        brand_img: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    listItem(newVal) {
      this.ruleForm.en_name = newVal.en_name
      this.ruleForm.zh_name = newVal.zh_name
      this.ruleForm.brand_name = newVal.brand_name
      this.ruleForm.brand_img = newVal.brand_img
      this.fileList = [this.handleList(newVal.brand_img)]
    }
  },
  methods: {
    handleList(url) {
      const oIndex = url.lastIndexOf('/')
      const name = url.slice(oIndex + 1)
      return { name, url }
    },
    show() {
      this.$refs.dialog.show()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.ruleForm.brand_img = file.url
      this.dialogVisible = true
    },
    handleSuccess(response) {
      if (response.code === Err_OK) {
        this.ruleForm.brand_img = response.data.file
      }
    },
    handleCloseDialog(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('confirm', this.ruleForm)
          this.clear()
          console.log(this.ruleForm)
        } else {
          this.$refs.dialog.show()
          return false
        }
      })
    },
    clear() {
      this.ruleForm = {
        en_name: '',
        zh_name: '',
        brand_name: '',
        brand_img: ''
      }
      this.fileList = []
    }
  }
}
</script>
<style lang="scss" scoped>
.el-input /deep/ .el-input__inner {
  width: 195px;
  height: 36px;
  border-radius: 3px;
  border: 1px solid rgba(226, 229, 234, 1);
}
.el-form-item /deep/ .el-icon-zoom-in {
  font-size: 26px;
  color: #fff;
}
.el-290 {
  &.el-input /deep/ .el-input__inner {
    width: 290px !important;
  }
}
.el-form-item /deep/ .el-icon-delete {
  font-size: 26px;
  color: #fff;
}
.color-red {
  color: red;
}
.dialog-wrapper /deep/ .el-dialog{
  min-width: 530px;
}
</style>
